﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace DOM_X10
{
    class Processor
    {
        private List<Tokken> lista;
        private byte[] respuesta = { 0, 85};

        public Processor(string [] sd)
        {
            
            foreach (String s in sd)
            {
                Lexer c = new Lexer(s);
                Tokken a = new Tokken();
                lista = new List<Tokken>();
                do
                {
                    c.Analize();
                    a = c.Item;
                    if(a.Value != "")
                    {
                        lista.Add(a);
                    }
                    Console.WriteLine(a.Value + "|" + a.Type + ":" + a.Subtype + "|" + a.Translate);
                }
                while (a.Value != "");

                procesaParametros();
            }
            Console.ReadLine();
        }

        private string procesaParametros()
        {
            //string cabDisp="0000/0100";
            string cabDisp = "00000100";
            string cabFunc="110";
            string codeFunc = "";

            string Result = "";
            Hashtable mapa = new Hashtable();
            
            lista.Reverse();
            foreach (Tokken x in lista)
            {
                switch (x.Type)
                {
                    case 0:
                        String translate;
                        if (mapa.ContainsKey(x.Subtype))
                        {
                            translate = (String)mapa[x.Subtype];
                            //translate = cabDisp + ":" + x.Translate + "|" + translate;
                            translate = cabDisp + x.Translate.ToString() + translate;
                        }
                        else
                        {
                            translate = "";
                            //translate = cabDisp + ":" + x.Translate + " " + cabFunc + ":" + x.codes[x.Subtype] + "/" + codeFunc + " ";
                            translate = cabDisp  + x.Translate.ToString() + cabFunc + x.codes[x.Subtype].ToString() + codeFunc;
                        }
                        mapa[x.Subtype] = translate;
                        break;
                    case 1:
                        if (x.Value.ToUpper() != "DIM")
                        {
                            //cabFunc = "00000" + "/" + cabFunc;
                            cabFunc = "00000" + cabFunc;
                        }

                        codeFunc = x.Translate.ToString();
                        break;
                    case 2:
                        //cabFunc = x.Translate.ToString() + "/" + cabFunc;
                        cabFunc = x.Translate.ToString() + cabFunc;
                        break;
                }
            }
            foreach (string partial in mapa.Values)
            {
                //Result += partial + "\n";
                Result += partial;
            }
            for (int i = 0; i < Result.Length; i++)
            {
                if (i % 8 == 0) Console.Write(' ');
                Console.Write(Result[i]);
            }
            Console.WriteLine();
            Console.WriteLine(Result);
            
            enviaSERIE(Result);
            
            return Result;
        }

        private void enviaSERIE(string comando)
        {
            SerialPort a = new SerialPort("COM1");
            a.Open();
            
            byte[] x=GetBytes(comando);
            int i = 0;
            while (i < x.Length)
            {
                Console.WriteLine(x[i].ToString() +":"+ x[i + 1].ToString());
                a.Write(x, i, 2);
                int checksum = x[i] + x[i+1];
                checksum %= 256;
                Console.WriteLine(checksum);
                int check = a.ReadByte();
                Console.WriteLine(check);
                if (checksum == check)
                {
                    a.Write(respuesta, 0, 1);
                    if (respuesta[1] == a.ReadByte())
                    {
                        i = i + 2;
                    }
                }
            }
            a.Close();

        }

        private byte[] GetBytes(string bitString)
        {
            return Enumerable.Range(0, bitString.Length / 8).
                Select(pos => Convert.ToByte(
                    bitString.Substring(pos * 8, 8),
                    2)
                ).ToArray();
        }
        private byte[] getBitwiseByteArray(string input)
        {
            List<byte> byteList = new List<byte>();
            for (int i = input.Length - 1; i >= 0; i -= 8)
            {
                string byteString = "";
                for (int j = 0; (i - j) >= 0 && j < 8; j++)
                {
                    byteString = input[i - j] + byteString;
                }
                if (byteString != "")
                {
                    byteList.Add(Convert.ToByte(byteString, 2));
                }
            }
            return byteList.ToArray();
        }

    }
}
